<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\App;
use think\response\Json;

class CourseHit extends Base
{
    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\CourseHit';

    //关联模型
    protected $hasOne = ['course', 'user'];

    public function index()
    {
        return $this->setTable();
    }

    public function setTable()
    {
        //构建table
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'id', 'title' => 'ID', 'sort' => true, 'width' => 80])
            ->addRow(['field' => 'title', 'title' => '课程标题', 'templet' => '<div>{{#  if(d.course){ }}{{d.course.title}}{{#  } }}</div>'])
            ->addRow(['field' => 'user', 'title' => '会员', 'templet' => '<div>{{#  if(d.user){ }}{{d.user.code}}{{#  } }}</div>'])
            ->addRow(['field' => 'create_time', 'title' => '点赞时间'])
            ->clearTable();

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch(), false)
            ->fetch();
    }

    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['course_id', '课程标题', 'input', '', '\app\common\model\Course', 'title,id'])
            ->setSearch(['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id']);
    }
}